import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-03-30
 * Time: 15:45
 */
public class TestIDBCDelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        //删除数据库中的记录
        //让用户输入一个id,根据id来进行删除
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.建立连接
        Connection connection = dataSource.getConnection();
        //3.用户输入id
        System.out.println("请输入要删除的id: ");
        int id = scanner.nextInt();
        //4.拼接SQL语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setInt(1,id);

        //5.执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret =" + ret);

        //6.回收释放资源
        statement.close();
        connection.close();
    }
}
